import { findAllCategory } from '@/api/category'
import { topCategory } from '@/api/constants'
// 分类模块
export default {
  namespaced: true,
  state () {
    return {
      // 分类信息集合
      list: topCategory.map(item => ({ name: item }))
    }
  },
  // 修改state数据
  mutations: {
    setList (state, headCategory) {
      state.list = headCategory
    },
    show (state, theItem) {
      const obj = state.list.find(item => item.id === theItem.id)
      obj.open = true
    },
    hide (state, theItem) {
      const obj = state.list.find(item => item.id === theItem.id)
      obj.open = false
    }
  },
  // 异步请求
  actions: {
    async getList (store) {
      const { result } = await findAllCategory()
      result.forEach(item => { item.open = false })
      store.commit('setList', result)
    }
  }
}
